package com.dap.configuration.web.app.controller;

import java.util.List;

import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baidu.disconf.web.service.app.bo.App;
import com.baidu.disconf.web.service.app.form.AppNewForm;
import com.baidu.disconf.web.service.app.service.AppMgr;
import com.baidu.disconf.web.web.app.validator.AppValidator;
import com.baidu.dsp.common.constant.WebConstants;
import com.baidu.dsp.common.controller.BaseController;
import com.baidu.dsp.common.vo.JsonObjectBase;
import com.dap.configuration.service.app.service.AppExtensionMgr;
import com.dap.configuration.service.app.vo.AppListExtensionVo;

/**
 * @author liaoqiqi
 * @version 2014-6-16
 */
@Controller
@RequestMapping(WebConstants.API_PREFIX + "/appExtension")
public class AppExtensionController extends BaseController {

    private static final Logger LOG = LoggerFactory.getLogger(AppExtensionController.class);

    @Autowired
    private AppMgr appMgr;
    
    @Autowired
    private AppExtensionMgr appExtensionMgr;
    
    @Autowired
    private AppValidator appValidator;
    
    /**
     * 获取app列表
     * @params null
     * @return JsonObjectBase
     * @throws ConfigException 
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public JsonObjectBase getAppList(String name){
    		List<AppListExtensionVo> list = appExtensionMgr.getAppList(name);
		    return buildListSuccess(list,list.size());
    }
    
    /**
     * 获取app
     * @params null
     * @return JsonObjectBase
     * @throws ConfigException 
     */
    @RequestMapping(value = "/getListByName", method = RequestMethod.GET)
    @ResponseBody
    public JsonObjectBase getAppByName(String name){
    		AppListExtensionVo vo = appExtensionMgr.getByName(name);
		    return buildSuccess(vo);
    }
    /**
     * 获取app
     * @params null
     * @return JsonObjectBase
     * @throws ConfigException 
     */
    @RequestMapping(value = "/{appId}", method = RequestMethod.GET)
    @ResponseBody
    public JsonObjectBase getAppById(@PathVariable long appId){
    		AppListExtensionVo vo = appExtensionMgr.getById(appId);
		    return buildSuccess(vo);
    }
    /**
     * create
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonObjectBase create(@Valid AppNewForm appNewForm) {

    	appValidator.validateCreate(appNewForm);

        App app = appMgr.create(appNewForm);

        return buildSuccess("创建成功appId="+app.getId());
    }
    
    /**
     * update
     * @return
     */
    @RequestMapping(value = "update", method = RequestMethod.PUT)
    @ResponseBody
    public JsonObjectBase update(@Valid AppListExtensionVo vo) {
    	AppNewForm appNewForm = new AppNewForm();
		appNewForm.setApp(vo.getName());
		appNewForm.setDesc(vo.getDescription());
		appNewForm.setEmails(vo.getEmails());
		appExtensionMgr.validateUpdate(vo.getId(),vo.getName());
        appExtensionMgr.update(vo.getId(),appNewForm,vo.getCreateTime());
        return buildSuccess("更新成功");
    }
    /**
     * 删除指定App
     * @params appId
     * @return JsonObjectBase
     * @throws  
     */
    @RequestMapping(value = "/{appId}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonObjectBase deleteAppById(@PathVariable long appId){
    		appExtensionMgr.validateDelete(appId);
    		appExtensionMgr.delete(appId);
    		return buildSuccess("删除成功");
    }
}

